<template>
  <page-header-wrapper>
    <a-card :bordered="false">

      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="名称">
                <a-input allow-clear placeholder="请输入名称" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="分类">
                <a-select v-model="queryParam.yaofangtypeid" placeholder="请选择分类" :default-active-first-option="false">
                  <a-select-option v-for="(item, index) in typeList" :key="item.id" :value="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-button type="primary" @click="handleSearch">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearch">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
        <a-button type="primary" style="margin-bottom: 8px" icon="plus" @click="addStore"> 添加药方</a-button>
      </div>

      <a-table ref="table" @change="tablePaginationChange" :pagination="pagination" bordered :dataSource="dataSource"
        :columns="columns" :rowKey="(record,index)=> index">
        <div slot="action" slot-scope="text, record" href="javascript:;">
          <a-button size='small' type="link" @click="showDetail(record)">
            详情
          </a-button>
          <a-divider type="vertical" />
          <a-button size='small' type="link" @click="updateInfo(record)">
            修改
          </a-button>

          <!-- <a-popconfirm title="是否要删除此行？" @confirm="deletInfo(record)">
            <a-button size='small' type="link" style="color: #fc2b2f">
              删除
            </a-button>
          </a-popconfirm> -->
        </div>
      </a-table>
    </a-card>
    <addBox ref="addBox" :visible="addFlag" @cancel="addCancel" @ok="AddSuccess">
    </addBox>
  </page-header-wrapper>
</template>

<script>
  import {
    STable,
    Ellipsis,
  } from '@/components'
  import {
    yaofangList,
    yaofangType,
    yaofangDetail
  } from '@/api/yaofang'
  import addBox from './add'

  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import {
    createVNode
  } from 'vue';
  import {
    Modal
  } from 'ant-design-vue';


  export default {
    components: {
      STable,
      Ellipsis,
      addBox,
    },
    data() {
      return {
        typeList: [],
        shenhe: 0, //审核
        status: 0, //状态
        addFlag: false,
        pagination: {
          current: 1, //默认当前叶数
          total: 0, // 数据总数 如果不设置就以绑定的数据量为准
          pageSize: 10, // 默认每页条数
          showSizeChanger: true, //是否可以改变 pageSize
          pageSizeOptions: ['10', '20', '40', '80', '100'], //可切换每叶数据条数
          showTotal: total => this.$t('共') + ' ' + `${total}` + ' ' + this.$t('条数据'), // 分页中显示总的数据
          hideOnSinglePage: false // 只有一页时是否隐藏分页器 true为不显示 false为显示
        },
        queryParam: {
          name: '',
          yaofangtypeid: ''
        },
        dataSource: [],
        columns: [{
            title: 'id',
            dataIndex: 'id',
            key: 'id',
            width: 100,
          },

          {
            title: '名称',
            dataIndex: 'name',
            key: 'name',
            ellipsis: true,
          },
          {
            title: '分类',
            dataIndex: 'yaofangtype',
            key: 'yaofangtype',
            ellipsis: true,
          },
          {
            title: '拼音首字母',
            dataIndex: 'pinyin',
            key: 'pinyin',
            ellipsis: true,
          },
          {
            title: '操作',
            key: 'operation',
            fixed: 'right',
            width: 190,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        searchflag: false
      }
    },
    created() {
      this.getList()
      this.getType()
    },
    methods: {
      showDetail(val) {
        yaofangDetail({
          id: val.id
        }).then(res => {
          this.addFlag = true
          this.$nextTick(() => {
            this.$refs.addBox.setPreview(res.data)
          })
        })
      },
      handleSearch() {
        this.pagination.current = 1
        this.searchflag = true
        this.getList()
      },
      resetSearch() {
        this.pagination.current = 1
        this.searchflag = false
        this.queryParam = {
          name: '',
          yaofangtypeid: ''
        }
        this.getList()
      },
      getType() {
        yaofangType().then(res => {
          this.typeList = res.data
        })
      },
      getList() {
        let data = {
          pageNo: this.pagination.current,
          pageSize: this.pagination.pageSize,
        }
        if (this.searchflag) {
          data = {
            pageNo: this.pagination.current,
            pageSize: this.pagination.pageSize,
            ...this.queryParam
          }
        }
        yaofangList(data).then(res => {
          this.pagination.total = res.data.totalCount
          this.dataSource = res.data.data
        })
      },
      updateInfo(val) {
        this.addFlag = true
        this.$nextTick(() => {
          this.$refs.addBox.setUpdate(val)
        })
      },

      addCancel() {
        this.addFlag = false
      },
      AddSuccess() {
        this.addFlag = false
        this.getList()
      },
      addStore() {
        this.addFlag = true
        this.$nextTick(() => {
          this.$refs.addBox.updateflag = false
        })
      },
      tablePaginationChange(e) {
        this.pagination = e
        this.getList()
      },
      getDate(stamp) {
        var date = new Date(stamp * 1000)
        return date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? ('0' + (date.getMonth() + 1)) : date.getMonth() +
          1) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
      },
      deletInfo(val) {
        medicineDelet(val.id).then(res => {
          this.getList()
        })
      }
    }
  }
</script>
<style scoped>
  .banner-img {
    width: 50px;
    height: 50px;
    border-radius: 5px;
  }
</style>